package com.rockxin.cat.dialog;

import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.util.SimpleArrayMap;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.orhanobut.logger.Logger;
import com.rockxin.cat.R;
import com.rockxin.cat.adapter.ChooseBankCardAdapter;
import com.rockxin.cat.base.User;
import com.rockxin.cat.bean.ChooseBankCardBean;
import com.rockxin.cat.network.CatNetWork;
import com.rockxin.cat.network.HttpResult;
import com.rockxin.cat.utils.ScreenUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import rx.Subscriber;

/**
 * <b>类名称：    ChooseBankCardDialog             <br/>
 * <b>类描述：                        <br/>
 * <b>创建人：    Lans               <br/>
 * <b>创建时间：  2018/3/2 17:23     <br/>
 * <b>修改人：                        <br/>
 * <b>修改时间：                      <br/>
 * <b>修改备注：                      <br/>
 *
 * @version 1.0.0 <br/>
 */
public class ChooseBankCardDialog extends DialogFragment implements BaseQuickAdapter.OnItemClickListener {

    private Context mContext;
    private List<ChooseBankCardBean> chooseBankCardBeanList;
    private String title;
    private ChooseBankCardAdapter mChooseBankCardAdapter;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context;
    }

    public static ChooseBankCardDialog newInstance(List<ChooseBankCardBean> chooseBankCardBeanList, String title) {
        ChooseBankCardDialog commentDialog = new ChooseBankCardDialog();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("ChooseBankCardList", (ArrayList<? extends Parcelable>) chooseBankCardBeanList);
        bundle.putCharSequence("title", title);
        commentDialog.setArguments(bundle);
        return commentDialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            chooseBankCardBeanList = getArguments().getParcelableArrayList("ChooseBankCardList");
            title = getArguments().getString("title");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(), R.style.AppTheme_NoDialogTitle);
        dialog.setCanceledOnTouchOutside(true);
        //填充对话框的布局
        View inflate = View.inflate(getActivity(), R.layout.dialog_choose_bank_card, null);
        //初始化控件
        initView(inflate);
        setDialogAttribute(dialog, inflate);
        return dialog;

    }

    /**
     * 设置dialog的属性
     *
     * @param dialog  dialog
     * @param inflate dialog的内容
     */
    private void setDialogAttribute(Dialog dialog, View inflate) {
        //将布局设置给Dialog
        dialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = dialog.getWindow();
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        //设置Dialog从窗体中间弹出
        lp.gravity = Gravity.CENTER;
        lp.height = LinearLayout.LayoutParams.WRAP_CONTENT;
        lp.width = (int) (ScreenUtils.getScreenWidth() * 0.8);
        dialogWindow.setAttributes(lp);
    }

    private void initView(View inflate) {
        TextView dialogTitle = inflate.findViewById(R.id.dialogTitle);
        dialogTitle.setText(title);

        RecyclerView recyclerView = inflate.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mChooseBankCardAdapter = new ChooseBankCardAdapter(chooseBankCardBeanList);
        recyclerView.setAdapter(mChooseBankCardAdapter);
        mChooseBankCardAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        dismiss();
        final ChooseBankCardBean bankCardBean = chooseBankCardBeanList.get(position);
        bankCardBean.setChoose(!bankCardBean.isChoose());
        mChooseBankCardAdapter.notifyDataSetChanged();
        final CatNetWork catNetWork = new CatNetWork();
       /* catNetWork.getFyConfig(new Subscriber<HttpResult>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(HttpResult httpResult) {
                if (httpResult.getStatus() == 1) {
                    HashMap<String, String> sparseArray = (HashMap<String, String>) httpResult.getData();
                    final String partner = sparseArray.get("partner");
                    final String key = sparseArray.get("key");
                    Logger.e("partner" + partner + "\nkey:" + key);
                    HashMap<String, String> hashMap = new HashMap<>();
                    hashMap.put("partner_no", partner);
                    hashMap.put("'uid'", User.getInstance().getToken());
                    catNetWork.recharge(hashMap, new Subscriber<HttpResult>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(HttpResult httpResult) {

                        }
                    });
                }
            }
        });*/
    }
}